﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.BLL.Extend;
using ZX.Web.UI.Controls;
using ZX.SYS.Library;
using ZX.CRM.Bussiness;
using ZX.CRM.Library;
using System.Collections;
namespace ZX.WebApp.EditWidows.SysManager
{
    public partial class SendAnnounce2 : BasePage
    {
            protected void Page_Load(object sender, EventArgs e)
            {
            }
            /// <summary>
            /// 保存按钮
            /// </summary>
            /// <param name="Subject">公告标题</param>
            /// <param name="Content">内容</param>
            /// <param name="ReceivedeptId">接收人Ids</param>
            /// <returns></returns>
            [AjaxPro.AjaxMethod]
            public string Save(string Subject, string Content, string planTime, string ReceivedeptId)
            {
                try
                {
                    //业务处理类
                    BaseBussnies Bbussness = new BaseBussnies();
                    BVuseremp userMrg = new BVuseremp();
                    int accLength = 0;
                    HttpFileCollection accfiles = ZXContext.Request.Files;
                    if (accfiles.Count > 0)
                    {
                        for (int i = 0; i < accfiles.Count; i++)
                        {
                            HttpPostedFile accpostFile = accfiles[i];
                            accLength += accpostFile.ContentLength;
                            if (accLength > 5 * 1024 * 1024)
                                ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script language='javascript'>alert('附件超过5M！')</script>");

                            //return;
                        }
                    }

                    Announce announce = new Announce();
                    V_UserEmp userInfo = userMrg.getUserInfoByUserId(this.UserId);
                    announce.ID = Guid.NewGuid();
                    announce.Subject = Subject;
                    announce.Content = Content;
                    announce.AddUser = (Guid)this.UserId;
                    announce.AddTime = DateTime.Now;
                    announce.SendDeptId = userInfo == null ? Guid.Empty : userInfo.DeptID;
                    announce.AddUserName = this.User.UName;
                    announce.IsOnTop = false;

                    AnnounceDepartment ad = new AnnounceDepartment();
                    ad.AnnounceID = announce.ID;
                    ad.DeptID = new Guid(ReceivedeptId);
                    Bbussness.Insert(announce);
                    Bbussness.Insert(ad);
                    //#region 选择接收部门
                    //string[] Depts = txt_AreaHidden1.Value.Split(',');
                    //if (Depts.Length > 0 && Depts[0].Length == 36)
                    //{
                    //    foreach (string us in Depts)
                    //    {
                    //        if (us.Length != 36) continue;
                    //        //InfoPlatDepartment dept=IDM.GetByID(new Guid(us));
                    //        //if(dept!=null)
                    //        //{
                    //        ad = new AnnounceDepartment();
                    //        ad.Announce = announce;
                    //        ad.DepartmentID = new Guid(us);
                    //        ad.SchoolID = SchoolID;
                    //        //}
                    //        //announce.Departments.Add(dept);
                    //        announce.AnnounceDepartments.Add(ad);
                    //    }
                    //}




                    return "1";
                }
                catch
                {
                    return "0";
                }
            }
            #region 附件管理
            /// <summary>
            /// 附件管理
            /// </summary>
            /// <param name="announce"></param>
            private void AccessoriesMangager(Announce announce)
            {
                ArrayList AccessoriesList = new ArrayList();

                //保存没有被清除的附件
                //if (announce.AnnounceAccessories.Count == AccessoriesHidden.Value.Length)
                //{

                //    for (int i = 0; i < announce.AnnounceAccessories.Count; i++)
                //    {
                //        if (AccessoriesHidden.Value.Substring(i, 1) == "T")
                //            AccessoriesList.Add(announce.AnnounceAccessories[i]);
                //    }
                //}

                //HttpFileCollection files = HttpContext.Current.Request.Files;
                //int length = 0;
                //for (int i = 0; i < files.Count; i++)
                //{
                //    HttpPostedFile postFile = files[i];
                //    if (postFile.FileName.Length < 1) continue;



                //    length += postFile.ContentLength;

                //    if (length > 5 * 1024 * 1024) throw new ApplicationException("附件超过5M！");

                //    //上传到文件
                //    Guid ID = Guid.NewGuid();
                //    string sTempFile = Staple.GetSaveFileRPath() + ID.ToString();
                //    postFile.SaveAs(sTempFile);
                //    //空文件放弃
                //    if (postFile.ContentLength < 1) continue;


                //    byte[] attachs = new byte[postFile.ContentLength];
                //    System.IO.Stream stream = postFile.InputStream;
                //    stream.Read(attachs, 0, postFile.ContentLength);
                //    stream.Close();

                //    //保存附件信息
                //    AnnounceAccessories announceAccessories = new AnnounceAccessories();
                //    announceAccessories.Announce = announce;
                //    announceAccessories.ID = ID;
                //    announceAccessories.Name = System.IO.Path.GetFileName(postFile.FileName);
                //    announceAccessories.Attach = attachs;

                //    AccessoriesList.Add(announceAccessories);
                //}


                //announce.AnnounceAccessories = AccessoriesList;

                //从arraylist 到数组
                //if(AccessoriesList.Count<1)return;
                //AnnounceAccessories[] AccessoriesArr=new AnnounceAccessories[AccessoriesList.Count];
                //for(int i=0;i<AccessoriesList.Count;i++)
                //	AccessoriesArr[i]=(AnnounceAccessories)AccessoriesList[i];


                //AnnounceManager AM=new AnnounceManager();
                ///AM.SetAccessories(announce.ID,AccessoriesArr);

            }
            #endregion
    }
}
